<template>
  <div class="body">
    <!-- <div class="container">
      <h1>Login</h1>
      <div class="txtb">
        <input
          type="text"
          v-model="phone"
          :class="{ focus: fo }"
          @focus="fo = true"
          @blur="bo"
        />
        <span data-placeholder="UserName"></span>
      </div>
      <div class="txtb">
        <input
          type="password"
          v-model="password"
          :class="{ focus: fou }"
          @focus="fou = true"
          @blur="bou"
        />
        <span data-placeholder="PassWord"></span>
      </div>
      <button>登录</button>
    </div> -->
    <el-form :model="form" status-icon :rules="rules" ref="form" class="demo-ruleForm container">
      <h1>宠物志后台</h1>
      <el-form-item>
        <div class="txtb">
          <input type="text" v-model="form.phone" :class="{ focus: fo }" @focus="fo = true" @blur="bo" />
          <span data-placeholder="账号"></span>
        </div>
      </el-form-item>
      <el-form-item>
        <div class="txtb">
          <input type="password" v-model="form.password" :class="{ focus: fou }" @focus="fou = true" @blur="bou" />
          <span data-placeholder="密码"></span>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import httpApi from "@/http";
import { mapMutations, mapState } from "vuex";
import { addroute1, addroute2 } from "@/router";
export default {
  data() {
    return {
      fo: false, // 绑定姓名关键字
      fou: false, // 绑定姓名关键字
      form: {
        phone: "",
        password: "",
      },
      rules: {
        phone: [
          { required: true, message: "请填写手机号", trigger: "blur" },
          {
            pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
            message: "3~15位字符,可以包含:数字、字母、下划线",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^\w{6,15}$/,
            message: "6~15位字符,可以包含:数字、字母、下划线",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    ...mapMutations(["saveUser", "saveToken", "saverights"]),
    bo() {
      if (this.form.phone == "") {
        this.fo = false;
      }
    },
    bou() {
      if (this.form.password == "") {
        this.fou = false;
      }
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          httpApi.userApi
            .loginByAdmin({
              admin_phone: this.form.phone,
              admin_pwd: this.form.password,
            })
            .then((res) => {
              console.log("登录账号", res);
              if (res.data.meta.code == 200) {
                this.saveUser(res.data.user);
                this.saveToken(res.data.token);
                this.saverights(res.data.rights);
                console.log(res.data.user[0].admin_level);
                if (res.data.user[0].admin_level == 1) {
                  addroute1();
                } else {
                  addroute2();
                }
                this.$message({
                  message: `登录成功`,
                  type: "success",
                });
                this.$router.push("/");
              } else {
                this.$message({
                  message: '用户名或密码错误,请重新输入',
                  type: 'danger'
                });
              }
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  padding: 0;
  margin: 0;
  text-decoration: none;
  box-sizing: border-box;
  font-family: montserrat;
}

.body {
  min-height: 100vh;
  background: url("./images/bg_admin.png");
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;

  >.container {
    width: 360px;
    background-color: #f1f1f1;
    height: 580px;
    padding: 80px 40px;
    border-radius: 10px;

    h1 {
      text-align: center;
      margin-bottom: 30px;
    }

    .txtb {
      border-bottom: 2px solid #adadad;
      position: relative;
      margin: 0 0 48px 0;

      input {
        font-size: 15px;
        color: #333;
        border: none;
        width: 100%;
        outline: none;
        background: none;
        padding: 0 5px;
        height: 40px;
      }

      &>span::before {
        content: attr(data-placeholder);
        position: absolute;
        top: 50%;
        left: 5px;
        color: #adadad;
        transform: translateY(-50%);
        z-index: 1;
        transition: 0.5s;
      }

      &>span::after {
        content: "";
        position: absolute;
        top: 100%;
        left: 0;
        width: 0%;
        height: 2px;
        background: linear-gradient(120deg, #3498db, #8e44ad);
        transition: 0.5s;
      }

      .focus+span::before {
        top: -5px;
      }

      .focus+span::after {
        width: 100%;
      }
    }

    button {
      display: block;
      width: 100%;
      height: 50px;
      border: none;
      background: linear-gradient(120deg, #3498db, #8e44ad, #3498db);
      background-size: 200%;
      color: #f1f1f1;
      outline: none;
      cursor: pointer;
      transform: 0.5;
      border-radius: 5px;

      &:active {
        opacity: 0.8;
      }
    }
  }
}
</style>
